package com.example.wechat.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.wechat.R;
import com.example.wechat.activity.SettingsActivity;
import com.example.wechat.activity.PaymentActivity;
import com.example.wechat.activity.FavoritesActivity;
import com.example.wechat.activity.AlbumActivity;
import com.example.wechat.activity.CardActivity;
import com.example.wechat.activity.StickerAlbumActivity;
import com.example.wechat.adapter.MeMenuAdapter;
import com.example.wechat.model.MeMenuItem;
import java.util.ArrayList;
import java.util.List;

public class MeFragment extends Fragment {
    private ImageView avatarImageView;
    private TextView nameTextView;
    private TextView idTextView;
    private RecyclerView menuRecyclerView;
    private MeMenuAdapter menuAdapter;
    private List<MeMenuItem> menuItems;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        
        // 初始化视图
        avatarImageView = view.findViewById(R.id.avatar_image);
        nameTextView = view.findViewById(R.id.name_text);
        idTextView = view.findViewById(R.id.id_text);
        menuRecyclerView = view.findViewById(R.id.menu_recycler_view);
        
        // 设置RecyclerView
        menuRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        menuItems = new ArrayList<>();
        menuAdapter = new MeMenuAdapter(menuItems);
        menuRecyclerView.setAdapter(menuAdapter);
        
        // 加载菜单数据
        loadMenuItems();
        
        // 设置点击事件
        menuAdapter.setOnItemClickListener(position -> {
            MeMenuItem item = menuItems.get(position);
            switch (item.getTitle()) {
                case "支付":
                    startActivity(new Intent(getContext(), PaymentActivity.class));
                    break;
                case "收藏":
                    startActivity(new Intent(getContext(), FavoritesActivity.class));
                    break;
                case "相册":
                    startActivity(new Intent(getContext(), AlbumActivity.class));
                    break;
                case "卡包":
                    startActivity(new Intent(getContext(), CardActivity.class));
                    break;
                case "表情":
                    startActivity(new Intent(getContext(), StickerAlbumActivity.class));
                    break;
                case "设置":
                    startActivity(new Intent(getContext(), SettingsActivity.class));
                    break;
            }
        });
        
        return view;
    }

    private void loadMenuItems() {
        menuItems.clear();
        menuItems.add(new MeMenuItem(R.drawable.ic_wallet, "支付", "", 0));
        menuItems.add(new MeMenuItem(R.drawable.ic_collect, "收藏", "", 0));
        menuItems.add(new MeMenuItem(R.drawable.ic_album, "相册", "", 0));
        menuItems.add(new MeMenuItem(R.drawable.ic_card, "卡包", "", 0));
        menuItems.add(new MeMenuItem(R.drawable.ic_emoji, "表情", "", 0));
        menuItems.add(new MeMenuItem(R.drawable.ic_settings, "设置", "", 0));
        menuAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 加载用户信息
        loadUserInfo();
    }

    private void loadUserInfo() {
        // TODO: 从本地或服务器加载用户信息
        // 这里使用默认值
        nameTextView.setText("微信用户");
        idTextView.setText("微信号：wxid_123456");
        
        // 加载头像
        Glide.with(this)
                .load(R.drawable.default_avatar)
                .circleCrop()
                .into(avatarImageView);
    }
} 